/*******************************************************************************
 * 文件名: JackJsonUtil.java
 * 描    述: JSON转换工具类
 * 创建人: Dean Huang(hudi-112@163.com)
 * 日　期: 2017-01-02
 * 描　述: 文件描述
 * 版　本: 1.0
 *******************************************************************************/

package com.z1.core;

import java.io.BufferedReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.z1.core.exception.Z1Exception;

public class JackJsonUtil {

	private static ObjectMapper objectMapper = new ObjectMapper();

	public static ObjectMapper getObjectMapper() {
		return objectMapper;
	}

	/**
	 * 将字符串转换为JsonNode
	 * 
	 * @param json
	 * @return
	 * @throws Exception
	 */
	public static JsonNode parseToJsonNodeFormString(String json) {
		return parseToTFromString(json, JsonNode.class);
	}

	/**
	 * 将对象转换为JsonNode
	 * 
	 * @param obj
	 * @return
	 */
	public static JsonNode parseToJsonNodeFromObject(Object obj) {
		return objectMapper.convertValue(obj, JsonNode.class);
	}

	/**
	 * 将reader转换为JsonNode
	 * 
	 * @param reader
	 * @return
	 * @throws Exception
	 */
	public static JsonNode parseToJsonNodeFromReader(BufferedReader reader) {
		try {
			return objectMapper.readValue(reader, JsonNode.class);
		}
		catch (Throwable e) {
			throw new Z1Exception(e);
		}
	}

	/**
	 * JsonNode转换为Map键值对
	 * 
	 * @param json
	 * @return
	 * @throws Exception
	 */
	public static Map<String, Object> toMapForJsonNode(JsonNode json) {
		return parseToMapFormString(toJSONString(json));
	}

	/**
	 * 将字符串转换为Map键值对
	 * 
	 * @param json
	 * @return
	 * @throws Exception
	 */
	public static Map<String, Object> parseToMapFormString(String json) {
		try {
			return objectMapper.readValue(json, new TypeReference<Map<String, Object>>() {});
		}
		catch (Throwable e) {
			throw new Z1Exception(e);
		}
	}

	/**
	 * 将reader转换为Map键值对
	 * 
	 * @param reader
	 * @return
	 * @throws Exception
	 */
	public static Map<String, Object> parseToMapFromReader(BufferedReader reader) {
		return toMapForJsonNode(parseToJsonNodeFromReader(reader));
	}

	/**
	 * 将字符串转换实体
	 * 
	 * @param json
	 * @param klass 实体Class
	 * @return
	 * @throws Exception
	 */
	public static <T> T parseToTFromString(String json, Class<T> klass) {
		try {
			return objectMapper.readValue(json, klass);
		}
		catch (Throwable e) {
			throw new Z1Exception("将" + json + "转换为json时出错.", e);
		}
	}
	
	/**
	 * 将字符串转换为实体列表
	 * @param json
	 * @param klass 实体Class
	 * @return
	 * @throws Exception
	 */
	public static <T> List<T> parseToTsFromString(String json, Class<T> klass) {
		try {
			JavaType javaType = objectMapper.getTypeFactory().constructParametricType(ArrayList.class, klass);
			return objectMapper.readValue(json, javaType);
		}
		catch (Throwable e) {
			throw new Z1Exception("将" + json + "转换为json时出错.", e);
		}
	}

	/**
	 * 将实体对象转换为json
	 * 
	 * @param obj
	 * @param klass
	 *            实体Class
	 * @return
	 */
	public static <T> T parseToTFromObject(Object obj, Class<T> klass) {
		return objectMapper.convertValue(obj, klass);
	}
	
	/**
	 * 对象转换为string
	 * 
	 * @param obj
	 * @return
	 * @throws Exception
	 */
	public static String toJSONString(Object obj) {
		return toJSONString(obj, false);
	}

	/**
	 * 对象转换为string 是否进行格式化输出
	 * 
	 * @param obj
	 * @param format
	 * @return
	 * @throws Exception
	 */
	public static String toJSONString(Object obj, boolean format) {
		try {
			objectMapper.configure(SerializationFeature.INDENT_OUTPUT, format);
			String json = objectMapper.writeValueAsString(obj);
			if (format) {
				objectMapper.configure(SerializationFeature.INDENT_OUTPUT, false);
			}
			return json;
		}
		catch (Throwable e) {
			throw new Z1Exception(e);
		}
	}

	public static ObjectNode createObjectNode() {
		return objectMapper.createObjectNode();
	}

	public static ArrayNode createArrayNode() {
		return objectMapper.createArrayNode();
	}
	
	/**
	 * 将字符串转换为Map List
	 * 
	 * @param json
	 * @return
	 * @throws Exception
	 */
	public static List<Map<String, Object>> parseToMapListFormString(String json) {
		try {
			return objectMapper.readValue(json, new TypeReference<List<Map<String, Object>>>(){});
		}
		catch (Throwable e) {
			throw new Z1Exception(e);
		}
	}
	
	/**
	 * 将对象数组转换为JsonNode List
	 * 
	 * @param objs
	 * @return
	 */
	public static List<JsonNode> parseToJsonNodeListFromObjectArray(Object[] objs) {
		List<JsonNode> list = new ArrayList<JsonNode>();
		for(Object obj : objs) {
			list.add(JackJsonUtil.parseToJsonNodeFromObject(obj));
		}
		return list;
	}
	
	/**
	 * 将对象List转换为JsonNode List
	 * 
	 * @param objs
	 * @return
	 */
	public static List<JsonNode> parseToJsonNodeListFromObjectList(List<?> objs) {
		List<JsonNode> list = new ArrayList<JsonNode>();
		for(Object obj : objs) {
			list.add(JackJsonUtil.parseToJsonNodeFromObject(obj));
		}
		return list;
	}

}
